home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / schematk / src_1152.lha / Agent.subproj / FindAgent.m < prev    next >
Encoding:
Text File  |  1992-03-16  |  5.5 KB  |  179 lines

  1. /* Copyright รก 1991 Gustavus Adolphus College.  All rights reserved.
  2.  *
  3.  * Schematik was developed by Gustavus Adolphus College (GAC) with
  4.  * support from NeXT Computer, Inc.  Permission to copy this software,
  5.  * to redistribute it, and to use it for any purpose is granted,
  6.  * subject to the following restrictions and understandings.
  7.  *
  8.  * 1. Any copy made of this software must include this copyright
  9.  * notice in full.
  10.  *
  11.  * 2. Users of this software agree to make their best efforts (a) to
  12.  * return to the GAC Mathematics and Computer Science Department any
  13.  * improvements or extensions that they make, so that these may be
  14.  * included in future releases; and (b) to inform GAC of noteworthy
  15.  * uses of this software.
  16.  *
  17.  * 3. All materials developed as a consequence of the use of this
  18.  * software shall duly acknowledge such use, in accordance with the
  19.  * usual standards of acknowledging credit in academic research.
  20.  *
  21.  * 4. GAC makes no express or implied warranty or representation of
  22.  * any kind with respect to this software, including any warranty
  23.  * that the operation of this software will be error-free.  ANY
  24.  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
  25.  * PURPOSE IS HEREBY DISCLAIMED.  GAC is under no obligation to
  26.  * provide any services, by way of maintenance, update, or otherwise.
  27.  *
  28.  * 5. In conjunction with products arising from the use of this
  29.  * material, there shall be no use of the name of Gustavus Adolphus
  30.  * College nor of any adaptation thereof in any advertising,
  31.  * promotional, or sales literature without prior written consent
  32.  * from GAC in each case.
  33.  */
  34.  
  35. #import "FindAgent.h"
  36. #import "../defines.h"
  37. #import DocText_h
  38. #import DocWin_h
  39. #import Main_h
  40. #import PrefAgent_h
  41. #import <appkit/Button.h>
  42. #import <appkit/Form.h>
  43. #import <appkit/Text.h>
  44. #import <mach.h>
  45. #import <string.h>
  46. extern void NXBeep(void);
  47.  
  48. #define NIBFILE        "Find.nib"
  49. #define FINDZONENAME    "FindAgnt"
  50. static id theAgent=nil;
  51.  
  52. @implementation FindAgent
  53.  
  54. + new
  55. {
  56. DEBUG_FUNC1(DEBUGLEVEL);
  57.     if (theAgent==nil)
  58.       {
  59.         NXZone *tempZone;
  60.  
  61.         tempZone = NXCreateZone(vm_page_size, vm_page_size, YES);
  62.         DEBUG_ASSERT(tempZone!=NULL)
  63.         theAgent = self = [super allocFromZone:tempZone];
  64.     agentZone = tempZone;
  65.     NXNameZone(agentZone, FINDZONENAME);
  66.       }
  67.     return theAgent;
  68. }
  69.  
  70. - activateAgent:sender
  71. {
  72. DEBUG_FUNC1(DEBUGLEVEL);
  73.     if (!findPanel)
  74.       {
  75.         findPanel = LoadNIB(NIBFILE, theAgent, agentZone);
  76.         DEBUG_ASSERT(findPanel!=nil)
  77.         [findPanel useOptimizedDrawing:YES];
  78.         EnableMenuItem(FindMenuCell3(MENU_EDIT, MENU_EDIT_FIND, MENU_EDIT_FIND_FindNext));
  79.         EnableMenuItem(FindMenuCell3(MENU_EDIT, MENU_EDIT_FIND, MENU_EDIT_FIND_FindPrevious));
  80.       }
  81.     [findText selectTextAt:0];
  82.     [findPanel makeKeyAndOrderFront:self];
  83.     return self;
  84. }
  85.  
  86. - findForward:sender
  87. {
  88. DEBUG_FUNC1(DEBUGLEVEL);
  89.     [msgText setStringValue:NULL];
  90.     [findText selectTextAt:0];
  91.     if (*[findText stringValueAt:0])
  92.       if (![[[NXApp mainWindow] textView] stringSearch:[findText stringValueAt:0] forAgent:self direction:FINDFORWARD ignoreCase:[ignoreCase state] wholeWord:[wholeWord state] inSel:NO])
  93.         {
  94.           NXBeep();
  95.           [msgText setStringValue:NOTFOUNDMESSAGE];
  96.           return self;
  97.         }
  98.     [[[NXApp mainWindow] textView] scrollSelToVisible];
  99.     return self;
  100. }
  101.  
  102. - findBackward:sender
  103. {
  104. DEBUG_FUNC1(DEBUGLEVEL);
  105.     [msgText setStringValue:NULL];
  106.     [findText selectTextAt:0];
  107.     if (*[findText stringValueAt:0])
  108.       if (![[[NXApp mainWindow] textView] stringSearch:[findText stringValueAt:0] forAgent:self direction:FINDBACKWARD ignoreCase:[ignoreCase state] wholeWord:[wholeWord state] inSel:NO])
  109.         {
  110.           NXBeep();
  111.           [msgText setStringValue:NOTFOUNDMESSAGE];
  112.           return self;
  113.         }
  114.     [[[NXApp mainWindow] textView] scrollSelToVisible];
  115.     return self;
  116. }
  117.  
  118. - replace:sender
  119. {
  120. DEBUG_FUNC1(DEBUGLEVEL);
  121.     [[[NXApp mainWindow] textView] replaceSel:[findText stringValueAt:1]];
  122.     [[NXApp mainWindow] setDocEdited:YES];
  123.     return self;
  124. }
  125.  
  126. - replaceAll:sender
  127. {
  128.     const char *message=REPLACEDMESSAGE;
  129.     char buffer[strlen(message)+16];
  130.     int count=0;
  131.     id text=[[NXApp mainWindow] textView];
  132.  
  133. DEBUG_FUNC1(DEBUGLEVEL);
  134.     [msgText setStringValue:NULL];
  135.     [findText selectTextAt:0];
  136.     if (*[findText stringValueAt:0])
  137.       {
  138.         NXSelPt dummy,bend,start;
  139.         [[NXApp mainWindow] disableFlushWindow];
  140.         [text saveSelectionState];
  141.         if (![scopeMatrix selectedRow])
  142.           [text selectAll:nil];
  143.         [text getSel:&dummy :&bend];
  144.         while ([text stringSearch:[findText stringValueAt:0] forAgent:self direction:FINDFORWARD ignoreCase:[ignoreCase state] wholeWord:[wholeWord state] inSel:YES])
  145.           {
  146.             [self replace:nil];
  147.             count++;
  148.             [text getSel:&start :&dummy];
  149.             [text setSel:start.cp :bend.cp];
  150.           }
  151.         DEBUG_ASSERT(![text stringSearch:[findText stringValueAt:0] forAgent:self direction:FINDFORWARD ignoreCase:[ignoreCase state] wholeWord:[wholeWord state] inSel:[scopeMatrix selectedRow]])
  152.         [text restoreSelectionState];
  153.         [[[NXApp mainWindow] reenableFlushWindow] flushWindow];
  154.         sprintf(buffer, message, count);
  155.         [msgText setStringValue:buffer];
  156.       }
  157.     return self;
  158. }
  159.  
  160. - replaceAndFind:sender
  161. {
  162. DEBUG_FUNC1(DEBUGLEVEL);
  163.     return [[self replace:nil] findForward:nil];
  164. }
  165.  
  166. @end
  167.  
  168. @implementation FindAgent (Delegate)
  169.  
  170. - textDidEnd:sender endChar:(unsigned short)theChar
  171. {
  172. DEBUG_FUNC1(DEBUGLEVEL);
  173.     if (theChar==NX_RETURN)
  174.       [findPanel close];
  175.     return self;
  176. }
  177.  
  178. @end
  179.